﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CNotebook;

namespace NotebookS2
{
    public partial class FND_CUSTINFO : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public FND_CUSTINFO()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }

        public int SearchIndex { get; set; }
        public string CardCode { get; set; }
        public string CardName { get; set; }
        public string BusinessPartnerType { get; set; }
        public string Address { get; set; }
        public string UnitNo { get; set; }
        public string Street { get; set; }
        public string Baranggay { get; set; }
        public string CityOrMunicipality { get; set; }
        public string PostalCode { get; set; }
        public string ProvinceOrMetro { get; set; }

        public string Fax { get; set; }
        public string Phone { get; set; }
        public string ContactPerson { get; set; }
        public string TIN { get; set; }


        void LoadByCode(string sCode)
        {
            try
            {
                DataTable dt = new DataTable();
                CustomerInfoManager oBPManager = new CustomerInfoManager();
                CustomerInfoUnit oBPUnit = new CustomerInfoUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByCardCode(sCode, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();

             
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        void LoadByName(string sName)
        {
            try
            {
                DataTable dt = new DataTable();
                AddressBookManager oBPManager = new AddressBookManager();
                AddressBookUnit oBPUnit = new AddressBookUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByBPName(sName, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();

             
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void LoadByStoreCode(string sStoreCode)
        {
            try
            {
                DataTable dt = new DataTable();
                AddressBookManager oBPManager = new AddressBookManager();
                AddressBookUnit oBPUnit = new AddressBookUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByStoreCode(sStoreCode, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void FND_CUSTINFO_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sSearch = txtSearch.Text + '%';
            try
            {
                if (cboSearch.SelectedIndex == 0)
                {
                    LoadByCode(sSearch);
                }
                else if (cboSearch.SelectedIndex == 1)
                {
                    LoadByName(sSearch);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            CustomerInfoManager oManager = new CustomerInfoManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dt= oManager.GetByCardCode(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["CardCode"]).ToString(), "Customer");

            oManager.Close();

            DataRow oRow = dt.Rows[0];

            this.CardCode = oRow["CardCode"].ToString();
            this.CardName = oRow["CardName"].ToString();
            
            this.TIN = oRow["TIN"].ToString();
            this.UnitNo = oRow["UnitNo"].ToString();
            this.Street = oRow["Street"].ToString();
            this.Baranggay = oRow["Brgy"].ToString();
            this.CityOrMunicipality = oRow["CityOrMunicipal"].ToString();
            this.PostalCode = oRow["PostalCode"].ToString();

            this.ProvinceOrMetro = oRow["ProvinceOrMetro"].ToString();
            this.Phone= oRow["Phone"].ToString();
            this.Fax = oRow["Fax"].ToString();
            this.ContactPerson = oRow["ContactPerson"].ToString();

            this.Close();

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            btnSearch_Click(sender, e);
        }
    }
}
